#include "mainwindow.h"
#include "ui_mainwindow.h"



MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    area[0][0] = ui->element0101;
    area[0][1] = ui->element0102;
    area[0][2] = ui->element0103;
    area[0][3] = ui->element0104;
    area[0][4] = ui->element0105;
    area[0][5] = ui->element0106;
    area[0][6] = ui->element0107;
    area[0][7] = ui->element0108;
    area[0][8] = ui->element0109;
    area[0][9] = ui->element0110;
    area[0][10] = ui->element0111;
    area[1][0] = ui->element0201;
    area[1][1] = ui->element0202;
    area[1][2] = ui->element0203;
    area[1][3] = ui->element0204;
    area[1][4] = ui->element0205;
    area[1][5] = ui->element0206;
    area[1][6] = ui->element0207;
    area[1][7] = ui->element0208;
    area[1][8] = ui->element0209;
    area[1][9] = ui->element0210;
    area[1][10] = ui->element0211;
    area[2][0] = ui->element0301;
    area[2][1] = ui->element0302;
    area[2][2] = ui->element0303;
    area[2][3] = ui->element0304;
    area[2][4] = ui->element0305;
    area[2][5] = ui->element0306;
    area[2][6] = ui->element0307;
    area[2][7] = ui->element0308;
    area[2][8] = ui->element0309;
    area[2][9] = ui->element0310;
    area[2][10] = ui->element0311;
    area[3][0] = ui->element0401;
    area[3][1] = ui->element0402;
    area[3][2] = ui->element0403;
    area[3][3] = ui->element0404;
    area[3][4] = ui->element0405;
    area[3][5] = ui->element0406;
    area[3][6] = ui->element0407;
    area[3][7] = ui->element0408;
    area[3][8] = ui->element0409;
    area[3][9] = ui->element0410;
    area[3][10] = ui->element0411;
    area[4][0] = ui->element0501;
    area[4][1] = ui->element0502;
    area[4][2] = ui->element0503;
    area[4][3] = ui->element0504;
    area[4][4] = ui->element0505;
    area[4][5] = ui->element0506;
    area[4][6] = ui->element0507;
    area[4][7] = ui->element0508;
    area[4][8] = ui->element0509;
    area[4][9] = ui->element0510;
    area[4][10] = ui->element0511;
    area[5][0] = ui->element0601;
    area[5][1] = ui->element0602;
    area[5][2] = ui->element0603;
    area[5][3] = ui->element0604;
    area[5][4] = ui->element0605;
    area[5][5] = ui->element0606;
    area[5][6] = ui->element0607;
    area[5][7] = ui->element0608;
    area[5][8] = ui->element0609;
    area[5][9] = ui->element0610;
    area[5][10] = ui->element0611;
    area[6][0] = ui->element0701;
    area[6][1] = ui->element0702;
    area[6][2] = ui->element0703;
    area[6][3] = ui->element0704;
    area[6][4] = ui->element0705;
    area[6][5] = ui->element0706;
    area[6][6] = ui->element0707;
    area[6][7] = ui->element0708;
    area[6][8] = ui->element0709;
    area[6][9] = ui->element0710;
    area[6][10] = ui->element0711;
    area[7][0] = ui->element0801;
    area[7][1] = ui->element0802;
    area[7][2] = ui->element0803;
    area[7][3] = ui->element0804;
    area[7][4] = ui->element0805;
    area[7][5] = ui->element0806;
    area[7][6] = ui->element0807;
    area[7][7] = ui->element0808;
    area[7][8] = ui->element0809;
    area[7][9] = ui->element0810;
    area[7][10] = ui->element0811;
    area[8][0] = ui->element0901;
    area[8][1] = ui->element0902;
    area[8][2] = ui->element0903;
    area[8][3] = ui->element0904;
    area[8][4] = ui->element0905;
    area[8][5] = ui->element0906;
    area[8][6] = ui->element0907;
    area[8][7] = ui->element0908;
    area[8][8] = ui->element0909;
    area[8][9] = ui->element0910;
    area[8][10] = ui->element0911;
    area[9][0] = ui->element1001;
    area[9][1] = ui->element1002;
    area[9][2] = ui->element1003;
    area[9][3] = ui->element1004;
    area[9][4] = ui->element1005;
    area[9][5] = ui->element1006;
    area[9][6] = ui->element1007;
    area[9][7] = ui->element1008;
    area[9][8] = ui->element1009;
    area[9][9] = ui->element1010;
    area[9][10] = ui->element1011;
    area[10][0] = ui->element1101;
    area[10][1] = ui->element1102;
    area[10][2] = ui->element1103;
    area[10][3] = ui->element1104;
    area[10][4] = ui->element1105;
    area[10][5] = ui->element1106;
    area[10][6] = ui->element1107;
    area[10][7] = ui->element1108;
    area[10][8] = ui->element1109;
    area[10][9] = ui->element1110;
    area[10][10] = ui->element1111;

    Labirynt l(0);
    console.setConsole(ui->text_area, area, l);
    createDictionary("dictionary.txt");

    connect(ui->actionNewGame, SIGNAL(triggered()), this, SLOT(newGame()));
    connect(ui->actionExit, SIGNAL(triggered()), qApp, SLOT(quit()));

    newGame();
    console.prepareConsole();
}



MainWindow::~MainWindow()
{
    delete ui;
}



void MainWindow::newGame()
{
    console.newGame();
}



void MainWindow::on_text_area_blockCountChanged(int newBlockCount)
{
    //enter i backspace

    if (console.thirdLastCommand() == console.getChooseHeroCommand())
    {
        console.setHeroName();
    }

    if (console.isCommand() && console.lastCommand().length() > 0)
    {
        bool understood = 0;
        QStringList check_sentences = console.lastCommand().split(".");
        QStringList check_screams;
        QStringList check_questions;
        QStringList check_phrases1;
        QStringList check_phrases2;
        QStringList check_phrases3;
        QStringList check_words;
        for (int i0 = 0; i0 < check_sentences.length(); i0++)
        {
            check_screams = check_sentences[i0].split("!");
            for (int i1 = 0; i1 < check_screams.length(); i1++)
            {
                check_questions = check_screams[i1].split("?");
                for (int i2 = 0; i2 < check_questions.length(); i2++)
                {
                    check_phrases1 = check_questions[i2].split(",");
                    for (int i3 = 0; i3 < check_phrases1.length(); i3++)
                    {
                        check_phrases2 = check_phrases1[i3].split(" i ");
                        for (int i4 = 0; i4 < check_phrases2.length(); i4++)
                        {
                            check_phrases3 = check_phrases2[i4].split(" a ");
                            for (int i5 = 0; i5 < check_phrases3.length(); i5++)
                            {
                                check_words = check_phrases3[i5].split(" ");
                                for (int h = 0; h < check_words.length(); h++)
                                {
                                    map<QString, QString>::iterator ih;
                                    for(ih = dictionary.begin(); ih != dictionary.end(); ih++)
                                    {
                                        if (check_words[h] == (*ih).first)
                                        {
                                            understood = 1;
                                            break;
                                        }
                                    }
                                    if (understood) break;
                                }
                                if (understood) break;
                            }
                            if (understood) break;
                        }
                        if (understood) break;
                    }
                    if (understood) break;
                }
                if (understood) break;
            }
            if (understood) break;
        }

        if (understood)
        {
            QStringList sentences = console.lastCommand().split(".");
            QStringList screams;
            QStringList questions;
            QStringList phrases1;
            QStringList phrases2;
            QStringList phrases3;
            QStringList words;
            for (int i0 = 0; i0 < sentences.length(); i0++)
            {
                screams = sentences[i0].split("!");
                for (int i1 = 0; i1 < screams.length(); i1++)
                {
                    questions = screams[i1].split("?");
                    for (int i2 = 0; i2 < questions.length(); i2++)
                    {
                        phrases1 = questions[i2].split(",");
                        for (int i3 = 0; i3 < phrases1.length(); i3++)
                        {
                            phrases2 = phrases1[i3].split(" i ");
                            for (int i4 = 0; i4 < phrases2.length(); i4++)
                            {
                                phrases3 = phrases2[i4].split(" a ");
                                for (int i5 = 0; i5 < phrases3.length(); i5++)
                                {
                                    words = phrases3[i5].split(" ");

                                    bool is_not = 0;
                                    int j0 = 0;
                                    while (j0 < words.length() && !is_not)
                                    {
                                        if (dictionary[words[j0]] == "not") is_not = 1;
                                        j0++;
                                    }
                                    if (is_not)
                                    {
                                        console.not();
                                        console.prepareCommand();
                                    }
                                    else
                                    {

                                        bool is_go_top = 0;
                                        bool is_move_else = 0;
                                        bool is_both = (is_go_top && is_move_else);
                                        int j1 = 0;
                                        int j2 = 0;
                                        while (j1 < words.length() && !is_both)
                                        {
                                            if (dictionary[words[j1]] == "goTop") is_go_top = 1;
                                            if (dictionary[words[j1]] == "turnRight" || dictionary[words[j1]] == "turnLeft" || dictionary[words[j1]] == "goDown")
                                            {
                                                is_move_else = 1;
                                                j2 = j1;
                                            }
                                            is_both = (is_go_top && is_move_else);
                                            j1++;
                                        }
                                        if (is_both)
                                        {
                                            console.callFunction(dictionary[words[j2]]);
                                            console.prepareCommand();
                                        }
                                        else
                                        {
                                            QString* functions = new QString[words.length()];

                                            for (int k = 0; k < words.length(); k++)
                                            {
                                                functions[k] = dictionary[words[k]];
                                                if (!anyFunctionEquals(dictionary[words[k]], functions, k))
                                                {
                                                    map<QString, QString>::iterator it;
                                                    for(it = dictionary.begin(); it != dictionary.end(); it++)
                                                    {
                                                        if (words[k] == (*it).first)
                                                        {
                                                            console.callFunction((*it).second);
                                                            console.prepareCommand();
                                                        }
                                                    }
                                                }
                                            }
                                            delete[] functions;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        else console.notUnderstand();
    }
}
